package cn.hchaojie.camp.day09;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class DemoJavafxGeneric extends Application {

	@Override
	public void start(Stage primaryStage) throws Exception {
//		Label label = new Label();
//		label.setText("123");
//		label.setText(String.valueOf(true));
		
//		NumberLabel label = new NumberLabel(18);
//		DoubleLabel label = new DoubleLabel(100.999);
		
		GenericLabel<Integer> label1 = new GenericLabel<Integer>(18);
		Integer message2 = label1.getMessage();
		GenericLabel<Double> label = new GenericLabel<Double>(18.999);
		Double message = label.getMessage();

//		GenericLabel<Boolean> label = new GenericLabel<Boolean>(true);
		// 如何限制参数类型
//		GenericLabel<Object> label = new GenericLabel<Object>(new Object());
		
		Scene scene = new Scene(label, 400, 300);

		primaryStage.setScene(scene);
		primaryStage.show();
	}

	public static void main(String[] args) {
		launch(args);
	}
	
	// 类的类型参数化
	// 限定类型参数是Number的子类
	class GenericLabel<T extends Number> extends Label {
		private T message;
		
		public GenericLabel(T i) {
			this.setMessage(i);
			
			setText(String.valueOf(getMessage()));
		}

		public T getMessage() {
			return message;
		}

		public void setMessage(T message) {
			this.message = message;
		}
	}
	
	// E -> Integer, Double
	class NumberLabel<E> extends Label {
		private E message;
		
		public NumberLabel(E i) {
			this.message = i;
			
			setText(String.valueOf(message));
		}
	}
//	
//	class DoubleLabel extends Label {
//		private Double message;
//		
//		public DoubleLabel(Double i) {
//			this.message = i;
//			
//			setText(String.valueOf(message));
//		}
//	}
}
